package cn.tedu.ssm.mapper;

import cn.tedu.ssm.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface UserMapper {
    /**
     * 分页查询数据
     *
     * @Param offset 数据偏离量
     * @Param limit 数据限制量
     */
    @Select("select id,`username`,`disabled` from `hbeu_user` where deleted = 0 limit #{o},#{l}")
    public List<User> find(@Param("o") int offset, @Param("l") int limit);


    @Select("select count(1) from `hbeu_user` where deleted = 0")
    public Long count();

    //1.检查账号是否存在
    //1.SELECT COUNT(1) FROM `user` WHERE username = 'Timor2'
    @Select("SELECT COUNT(1) FROM `hbeu_user` WHERE username = #{u}")
    public boolean exists(@Param("u") String username);

    //2.根据账号和密码查找其ID
    //2.SELECT id FROM `user` WHERE username = 'Timor2' AND `password` = MD5('123')
    @Select("SELECT id FROM `hbeu_user` WHERE username = #{u} AND `password` = #{p}")
    public Long findIdByUsernameAndPassword(@Param("u") String username, @Param("p") String password);

    //3.根据ID检查账号是否已经被删除
    //3.SELECT COUNT(1) FROM `user` WHERE id = 5 AND `deleted` = 1
    @Select("SELECT COUNT(1) FROM `hbeu_user` WHERE id = #{i} AND `deleted` = 1")
    public boolean isDeleted(@Param("i") Long id);

    //4.根据ID检查账号是否已经被禁用
    //4.SELECT COUNT(1) FROM `user` WHERE id = 5 AND `disabled` = 1
    @Select("SELECT COUNT(1) FROM `hbeu_user` WHERE id = #{i} AND `disabled` = 1")
    public boolean isDisabled(@Param("i") Long id);

    /**
     * 据用户id查询用户姓名
     * @param id:用户id
     */
    @Select("SELECT `name` FROM `hbeu_user` where id = #{id}")
    String findNameById(@Param("id")Long id);

    /**
     * 查询所有非删除用户的姓名和禁用状态
     */
    @Select("select `name`,disabled from `hbeu_user` where deleted = 0;")
    List<User> findNameAndDisabled();

    /**
     * 根据用户名和姓名增加用户
     * @param name:姓名
     * @param username:用户名
     * @return 返回用户增加的数量
     */
    @Insert("insert into `hbeu_user`(`name`,`username`,`pwd`) values(#{name},#{username},#{pwd});")
    Integer userAdd(@Param("name") String name,@Param("username") String username,@Param("pwd") String pwd);

    /**
     * 根据用户姓名查询用户id
     * @param username:用户姓名
     * @return 用户id
     */
    @Select("SELECT COUNT(1) ,id FROM `hbeu_user` WHERE username=#{username};")
    User findIdByUsername(@Param("username") String username);

    @Select(" SELECT COUNT(1) , `name`, `username` FROM `hbeu_user` WHERE id=#{id}")
    User findInfoById(@Param("id")Long id);

    @Update(" UPDATE `users` SET `name`=#{name},`pwd`=MD5(#{pwd}) WHERE id=#{id};")
    Long updateUser(@Param("id")Long id, @Param("name") String name, @Param("id") String pwd);
}
